package com.qxl.forest.business.persistence.dao;

import com.qxl.forest.business.persistence.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: qiuxinlin
 * @Dercription:
 * @Date: 19:12 2017/11/23
 */
@Repository
public interface UserDao extends PagingAndSortingRepository<User, Integer> {
    /**
     * 分页查询用户
     *
     * @param pageable
     * @return
     */
    @Override
    Page<User> findAll(Pageable pageable);

    /**
     * 通过账号查询用户
     *
     * @param account
     * @return
     */
    User findByAccount(String account);

    /**
     * 冻结/解除冻结
     *
     * @param userId
     * @param status
     */
    @Transactional //用于提交事物
    @Modifying //使ORM知道现在要执行的是写操作
    @Query(value = "UPDATE f_user SET status=?2 WHERE id=?1", nativeQuery = true)
    void setStatus(Integer userId, int status);

    /**
     * 用户角色分配
     *
     * @param userId
     * @param roleIds
     */
    @Transactional
    @Modifying
    @Query(value = "update f_user set roleid=?2 where id=?1", nativeQuery = true)
    void setRoles(Integer userId, String roleIds);

}
